package leetcode.code117;

import java.util.ArrayList;
import java.util.List;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;
import leetcode.helper.tree.Node;

public abstract class Solution117 implements IDebug {

	public abstract Node connect(Node root);

	@Override
	public void debug1() {
//		输入：root = [1,2,3,4,5,null,7]
//		输出：[1,#,2,3,#,4,5,7,#]
		String string = "[1,2,3,4,5,null,7]";
		String string1 = "[1,#,2,3,#,4,5,7,#]";
		Node root = Node.array2tree(HelpDebug.str2array4(string));
		Node result = this.connect(root);
//		Node expect = Node.array2tree(HelpDebug.str2array6(string1));
		this.print(result);
	}

	@Override
	public void debug2() {
		String string = "[1,2,3,4,null,null,5]";
		Node root = Node.array2tree(HelpDebug.str2array4(string));
		Node result = this.connect(root);
		this.print(result);
	}

	private void print(Node root) {
		List<String> list = new ArrayList<>();
		Node head = root;
		while (head != null) {
			Node p = head;
			if (head.left != null) {
				head = head.left;
			} else {
				head = head.right;
			}
			while (p != null) {
				list.add(String.valueOf(p.val));
				if (head == null && p.left != null) {
					head = p.left;
				}
				if (head == null && p.right != null) {
					head = p.right;
				}
				if (p.next == null) {
					list.add("#");
					p = null;
				} else
					p = p.next;
			}
		}
		for (String s : list) {
			System.out.print(s + ",");
		}
		System.out.println(" ");
	}

	@Override
	public void debug3() {
		//
		String string = "[1,2,3,4,5,null,6,7,null,null,null,null,8]";
		// [1,#,2,3,#,4,5,6,#,7,8,#]
		Node root = Node.array2tree(HelpDebug.str2array4(string));
		Node result = this.connect(root);
		this.print(result);

	}

	@Override
	public void debug4() {
		//
		String string = "[2,1,3,0,7,9,1,2,null,1,0,null,null,8,8,null,null,null,null,7]";
		// [1,#,2,3,#,4,5,6,#,7,8,#]
		Node root = Node.array2tree(HelpDebug.str2array4(string));
		Node result = this.connect(root);
		this.print(result);

	}

}
